#include "location.h"
#include <iostream>
#include <fstream>
#include "Dungeon.h"
#include "Item.h"
#include "character.h"

// Implementation file

/*the private members are:
		int height
		int width
		location** room
*********************************************************************************************************************************/
//the specified dungeon object is created with the layout specified in the file
Dungeon :: Dungeon(string file )
{
	ifstream infile;
	infile.open(file.c_str());
	infile>>height>>width;
	
	int i;
	room = new location*[height];
	for(i = 0; i < height; i++)
		room[i] = new location[width];

	int in;
	for(i = 0;i < height; i++)
		for (int j = 0; j < width; j++)
		{
			infile>>in;
			if(in == 1)
			{
				room [i][j].setWall(true);
			}
		}
	

	infile.close();
	
}
/*********************************************************************************************************************************/
// the specified dungeon object is created with a width and height specified by arguments
Dungeon :: Dungeon(int rows,int cols)
{
	ifstream infile;
	infile.open("dungeon.txt");
	infile>>rows>>cols;
	height = rows;
	width =  cols;
	//Character* player_temp;
	int temp;
		
	room = new location*[height];

	for(int j=0; j< height; j++)
		room[j]= new location[width];

//KNOWN ISSUE!  So far our dungeon is perfectly well lain out.  This should be fixed by tomorrow!  It should read where 1s and 0s
//are not just 0s!  Fix this.

	for(int r = 0; r < height; r++)
		for (int c = 0; c < width; c++)
		{
			infile>>temp;
	
            if(temp == 1)
                 room[r][c].setWall(true);
         } 
	infile.close();
}
/*********************************************************************************************************************************/
// the specified Dungeon object is deallocated
Dungeon ::~Dungeon()
{
	for(int i=0; i< height; i++)
		delete [] room[i];
	delete [] room;
}
/*********************************************************************************************************************************/
location&  Dungeon :: getRoom(int row,int col)
{
	return room[row][col];
}
/*********************************************************************************************************************************/
int  Dungeon :: getHeight()
{
	return height;
}
/*********************************************************************************************************************************/
int  Dungeon :: getWidth()
{
	return width;
}
/*********************************************************************************************************************************/
// The contents and layout of the dungeon are displayed to the screen
void Dungeon::display()
{
	int i, j;
	
	// Display the top border. 
	cout << " " << (char)201;
	for(j = 0; j < width; j++) 
	{ 
		cout << (char)205 << (char)205 << (char)205; 
		if(j != width - 1) 
		{ 
			cout << (char)203;
		} 
	} 
	cout << (char)187 << endl; 
	// Display the interior. 
	for(i = 0; i < height; i++) 
	{ 
		cout << " " << (char)186;
		for(j = 0; j < width; j++) 
		{ 
			if(room[i][j].getPlayer() != NULL) 
			{ 
				if(room[i][j].getItem() !=NULL)
				{
					if(room[i][j].getItem()->getName() == "Golden Scepter")
						cout<<(char)2<< " " << (char)18;
					else if(room[i][j].getItem()->getName() == "Emerald Scarab")
						cout<<(char)2<< " " << (char)15;
					else if(room[i][j].getItem()->getName() == "Ankh")
						cout<<(char)2<< " " << (char)12;
					else if(room[i][j].getItem()->getName() == "unknown")
					     cout<<(char)2<<" "<<" ";
				}
				else
					cout << (char)2 << "  "; 
			} 
			else if(i == height-1 && j == width-1)
				cout << "* *";
			else if(room[i][j].getItem() !=NULL)
			{
					if(room[i][j].getItem()->getName() == "Golden Scepter")
						cout<<"  " << (char)18;
					else if(room[i][j].getItem()->getName() == "Emerald Scarab")
						cout<<"  " << (char)15;
					else if(room[i][j].getItem()->getName() == "Ankh")
						cout<<"  " << (char)12;
					else if(room[i][j].getItem()->getName() == "unknown")
                         cout<<"   ";
			}
			else 
			{ 
				cout << "   "; 
			}

			if(j < width - 1 && (room[i][j].getWall() || room[i][j + 1].getWall())) 
			{
				cout << (char)186; 
			} 
			else if(j == width - 1) 
			{ 
				cout << (char)186; 
			} 
			else 
			{ 
				cout << (char)208; 
			} 
		} 
		cout << endl; 
		cout << " " << (char)186; 
		for(j = 0; j < width; j++) 
		{ 
			if(i == height-1 && j == width-1)
				cout<<"* *"<< (char)186;
			else
			{
			cout << "   "; 
			if(j < width - 1 && (room[i][j].getWall() || room[i][j + 1].getWall())) 
			{ 
				cout << (char)186; 
			} 
			else if(j == width - 1) 
			{ 
				cout << (char)186; 
			} 
			else 
			{ 
				cout << (char)210; 
			}
			}
		} 
		cout << endl;
		if(i != height - 1) 
		{ 
			cout << " " << (char)204; 
			for(j = 0; j < width; j++)
			{ 
				if(i < height - 1 && (room[i][j].getWall() || room[i + 1][j].getWall())) 
				{ 
					cout << (char)205 << (char)205 << (char)205; 
					if(j == width - 1) 
					{ 
						cout << (char)185; 
					} 
					else 
					{
						cout << (char)206; 
					}
				}
				else if(j == width - 1) 
				{
					cout << (char)181 << " " << (char)198; 
					cout << (char)185; 
				}
				else 
				{
					cout << (char)181 << " " << (char)198; 
					cout << (char)206; } } cout << endl; 
		}
	}
	// Display the bottom border.
	cout << " " << (char)200; 
	for(j = 0; j < width; j++) 
	{
		cout << (char)205 << (char)205 << (char)205;
		if(j != width - 1) 
		{
			cout << (char)202; 
		}
	} 
	cout << (char)188 << endl;

}
